// pages/consult/consultDetails/evaluation/evaluation.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    consultantId: null,
    evaluationList: [],
    url: '' 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      consultantId: options.consultantId,
      url: app.globalData.url 
    });
    this.getEvaluationList();
  },
  
  getEvaluationList() {
    const { consultantId, url } = this.data;
    wx.request({
      url: url + 'consultantEvaluation/consultant/' + consultantId, 
      method: 'GET',
      success: (res) => {
        console.log(res);
        if (res.data && res.data.code === 200) {
          const formatList = res.data.data.map(item => ({
            ...item,
            evaluationTimeFormat: this.formatTime(item.evaluationTime)
          }));
          this.setData({ evaluationList: formatList });
        } else {
          wx.showToast({
            title: '评价数据加载失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      }
    });
  },

  formatTime(dateStr) {
    const today = new Date();
    const evaluationDate = new Date(dateStr);
    const diffDays = Math.floor((today - evaluationDate) / (1000 * 60 * 60 * 24));
    
    if (diffDays === 0) return '今天';
    if (diffDays === 1) return '昨天';
    if (diffDays < 7) return diffDays + '天前';
    return evaluationDate.getFullYear() + '-' + 
    (evaluationDate.getMonth() + 1).toString().padStart(2, '0') + '-' + 
    evaluationDate.getDate().toString().padStart(2, '0');
  },

  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {}
})
